package com.lp.multi.lock;

import java.util.concurrent.TimeUnit;

/**
 * @author: lp
 * @create: 2021-06-26 21:05
 * @description: 八锁现象1
 * 主线程sleep足够时间(1秒)，A线程先拿到phone对象的锁
 * 执行结果:
 * A:send msg
 * B:call msg
 * 原因:主线程sleep期间，B线程还未创建，A线程先运行拿到了锁
 */
public class BLock {
    public static void main(String[] args) {
        Phone1 phone = new Phone1();

        new Thread(()->{
                phone.sendMsg();
        },"A").start();

        //未获取锁前睡眠
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(()->{
                phone.call();
        },"B").start();
    }
}



class Phone1 {

    public synchronized void sendMsg(){
        System.out.println(Thread.currentThread().getName()+":"+"send msg");
    }

    public synchronized void call(){
        System.out.println(Thread.currentThread().getName()+":"+"call msg");
    }

}
